package com.dao.impl;

import com.bean.Menu;
import com.bean.Role;
import com.bean.Users;
import com.dao.DBUtils;
import com.dao.RolesDao;
import com.service.RolesService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RolesDaoImpl extends DBUtils implements RolesDao {
    @Override
    public List<Role> getRolesList(int pageIndex, int pageSize) {
        List<Role> roleList = new ArrayList<Role>();
        String sql="select * from role limit ?,?";
        try {
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            resultSet = query(sql,params);
            while (resultSet.next()){
                //1.取出各表数据

                Role role = new Role();
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleState(resultSet.getInt("rolestate"));
                //2.建立关系
                roleList.add(role);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return  roleList;
    }

    @Override
    public int total() {
        int total = 0;
        String sql = "select count(1) from role  ";
        try {
            resultSet = query(sql, null);
            while (resultSet.next()) {
                total = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return total;
    }

    @Override
    public int insert(Role role) {
        int key = 0;
        try {
            String sql = "insert into role value(null,?,?)";
            List params = new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
             int update = update(sql,params);

            ResultSet generatedKeys = pps.getGeneratedKeys();
            if(generatedKeys.next()){
                key = generatedKeys.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return key;
    }

    @Override
    public Role findbyid(int roleId) {
        Role role = new Role();
        List<Menu> menuList = new ArrayList<>();
        try {
            String sql = "select * from role r,menu m,middle mid where r.roleid=mid.roleid and mid.menuid=m.menuid and r.roleid=?";
            List params= new ArrayList();
            params.add(roleId);
            resultSet = query(sql,params);
            while (resultSet.next()){
                Menu menu = new Menu();
                role.setRoleState(resultSet.getInt("rolestate"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleId(resultSet.getInt("roleid"));
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUrl(resultSet.getString("url"));
                menu.setState(resultSet.getInt("state"));
                menu.setUpMenuId(resultSet.getInt("upmenuid"));
                menuList.add(menu);
            }
            role.setMenuList(menuList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return role;
    }

    @Override
    public int updateRole(int roleId, String roleName, int roleState) {
        int update = 0;
        try {
            String sql = "update role set rolename=?,rolestate=? where roleid=?";
            List params = new ArrayList();
            params.add(roleName);
            params.add(roleState);
            params.add(roleId);
            update = update(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public int delete(int roleId) {
        int del = 0;
        try {
            String sql = "delete from role where roleid=?";
            List params = new ArrayList();
            params.add(roleId);
            del = update(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return del;
    }


}
